﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace PrototypeUserControl
{
    public partial class wucProduct : System.Web.UI.UserControl
    {

        public string IdProduct
        {
            get { return this.txtID.Text; }
            set
            {
                this.txtID.Text = value;
                this.showReg();
            }
        }

        public string HeaterText
        {
            get { return this.header.Text; }
            set { this.header.Text = value; }
        }

        private void showReg()
        {
            string conStr = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123";

            System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(conStr);

            try
            {
                sqlcon.Open();

                string strSQL = " select * from Products where idProducts=" + this.IdProduct;

                System.Data.SqlClient.SqlCommand dbcomm = new System.Data.SqlClient.SqlCommand(strSQL, sqlcon);

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                da.SelectCommand = dbcomm;



                DataTable dt = new DataTable();
                da.Fill(dt);


                this.txtName.Text = dt.Rows[0][1].ToString();
                this.txtUnit.Text = dt.Rows[0][2].ToString();
                this.txtSupplier.Text = dt.Rows[0][3].ToString();
                this.btnSubmit.Text = "Update";



                da.Dispose();
                sqlcon.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                sqlcon.Close();
            }
        }

        private void InsertUpdateDelete()
        {
            //more coding here
        }

        public void NewRegister()
        {
            //Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
            string conStr = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123";

            System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(conStr);
            try
            {
                sqlcon.Open();

                string strSQL = "insert into Products(name, units, supplier) values('" + this.txtName.Text + "', " + this.txtUnit.Text + ", '" + this.txtSupplier.Text + "')";
                //" select * from Products "
                System.Data.SqlClient.SqlCommand dbcomm = new System.Data.SqlClient.SqlCommand(strSQL, sqlcon);

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                da.SelectCommand = dbcomm;

                dbcomm.ExecuteNonQuery();
                da.Dispose();
                sqlcon.Close();

                this.Response.Redirect(this.Request.Path, false);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                sqlcon.Close();
            }
        }

        public void UpdateRegister()
        {
            //Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTemp.mdf;Integrated Security=True;User Instance=True"
            string conStr = "Data Source=72.18.135.49,1533;Initial Catalog=DBGio;Persist Security Info=True;User ID=gio;Password=admin123";

            System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection(conStr);
            try
            {
                sqlcon.Open();

                string strSQL = "update Products set name='" + this.txtName.Text + "', units='" + Convert.ToInt64(this.txtUnit.Text) + "', supplier='" + this.txtSupplier.Text + "' where idProducts=" + this.txtID.Text;
                System.Data.SqlClient.SqlCommand dbcomm = new System.Data.SqlClient.SqlCommand(strSQL, sqlcon);

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                da.SelectCommand = dbcomm;

                dbcomm.ExecuteNonQuery();
                da.Dispose();
                sqlcon.Close();
                this.Response.Redirect(this.Request.Path, false);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
            finally
            {
                sqlcon.Close();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (this.txtID.Text != string.Empty)
            {
                this.UpdateRegister();
            }
            else
            {
                this.NewRegister();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtName.Text = "";
            this.txtUnit.Text = "";
            this.txtSupplier.Text = "";

        }
    }
}